home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / programer2 / pari2 / pari / other / version < prev    next >
Text File  |  1991-11-28  |  4KB  |  193 lines

  1. #include "genpari.h"
  2.  
  3. void printversion()
  4. {
  5.   pariputs("             GP/PARI CALCULATOR Version 1.36\n");
  6.   pariputs("                 (portable 32-bit version)\n");
  7. }
  8.  
  9. ulong overflow,hiremainder;
  10.  
  11. int addll(x,y)
  12.      int x,y;
  13. {
  14.   int z;
  15.  
  16.   z=x+y;
  17.   if((x>=0)&&(y>=0)) {overflow=0;return z;}
  18.   if((x<0)&&(y<0)) {overflow=1;return z;}
  19.   overflow=(z>=0)?1:0; return z;
  20. }
  21.  
  22. int addllx(x,y)
  23.      int x,y;
  24. {
  25.   int z;
  26.  
  27.   z=x+y+overflow;
  28.   if((x>=0)&&(y>=0)) {overflow=0;return z;}
  29.   if((x<0)&&(y<0)) {overflow=1;return z;}
  30.   overflow=(z>=0)?1:0; return z;
  31. }
  32.  
  33. int subll(x,y)
  34.      int x,y;
  35. {
  36.   int z;
  37.  
  38.   z=x-y;
  39.   if((x>=0)&&(y<0)) {overflow=1;return z;}
  40.   if((x<0)&&(y>=0)) {overflow=0;return z;}
  41.   overflow=(z>=0)?0:1; return z;
  42. }
  43.  
  44. int subllx(x,y)
  45.      int x,y;
  46. {
  47.   int z;
  48.  
  49.   z=x-y-overflow;
  50.   if((x>=0)&&(y<0)) {overflow=1;return z;}
  51.   if((x<0)&&(y>=0)) {overflow=0;return z;}
  52.   overflow=(z>=0)?0:1; return z;
  53. }
  54.  
  55. int shiftl(x,y)
  56.      ulong x,y;
  57. {
  58.   hiremainder=x>>(32-y);return (x<<y);
  59. }
  60.  
  61. int shiftlr(x,y)
  62.      ulong x,y;
  63. {
  64.   hiremainder=x<<(32-y);return (x>>y);
  65. }
  66.  
  67. int bfffo(x)
  68.      ulong x;
  69. {
  70.   int sc;
  71.   static int tabshi[16]={4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
  72.  
  73.   if(x&(0xffff0000)) sc=0;else {sc=16;x<<=16;}
  74.   if(!(x&(0xff000000))) {sc+=8;x<<=8;}
  75.   if(x&(0xf0000000)) x>>=28;else {sc+=4;x>>=24;}
  76.   sc+=tabshi[x];return sc;
  77. }
  78.  
  79. int mulll(x,y)
  80.      ulong x,y;
  81. {
  82.   ulong xlo,xhi,ylo,yhi;
  83.   ulong z;
  84.  
  85.   xlo=x&65535;xhi=x>>16;ylo=y&65535;yhi=y>>16;
  86.   z=addll(xlo*yhi,xhi*ylo);
  87.   hiremainder=(overflow)?xhi*yhi+65536+(z>>16):xhi*yhi+(z>>16);
  88.   z=addll(xlo*ylo,(z<<16));hiremainder+=overflow;
  89.   return z;
  90. }
  91.  
  92. int addmul(x,y)
  93.      ulong x,y;
  94. {
  95.   ulong xlo,xhi,ylo,yhi;
  96.   ulong z,z2;
  97.  
  98.   xlo=x&65535;xhi=x>>16;ylo=y&65535;yhi=y>>16;
  99.   z=addll(xlo*yhi,xhi*ylo);
  100.   z2=(overflow)?xhi*yhi+65536+(z>>16):xhi*yhi+(z>>16);
  101.   z=addll(xlo*ylo,(z<<16));z2+=overflow;
  102.   z=addll(z,hiremainder);hiremainder=z2+overflow;
  103.   return z;
  104. }
  105.  
  106. /* Ancienne version de divll, en generale plus lente sauf si le processeur flottant
  107. est tres rapide
  108.  
  109. int divll(x,y)
  110.      ulong x,y;
  111. {
  112.   ulong p,p1,p2;
  113.   long p3,p4;
  114.   double dbl;
  115.  
  116.   if((ulong)hiremainder>=y) err(divller1);
  117.   p1=hiremainder;dbl=(C4*p1+x)/y;
  118.   if(dbl < C4/2) p=(ulong)dbl;
  119.   else
  120.     {
  121.       if(dbl>C4-1) p=0xffffffff;
  122.       else{dbl-=(C4/2);p=(ulong)dbl;p|=0x80000000;}
  123.     }
  124.   p2=mulll(p,y);p3=x-p2;
  125.   if(x<p2) hiremainder++;
  126.   if((p1==(ulong)hiremainder)&&((ulong)p3<y))
  127.     {hiremainder=p3;return p;}
  128.   if(p1<(ulong)hiremainder) {hiremainder=p3+y;return p-1;}
  129.   hiremainder=p3-y;return p+1;
  130. }
  131.  
  132. */
  133.  
  134. int divll(x,y)
  135.      ulong x,y;
  136. {
  137. #define HIBIT 0x80000000
  138. #define HIMASK 0xffff0000
  139. #define LOMASK 0xffff
  140. #define HIWORD(a) (a >> 16)
  141. /* si le compilateur est bugge, il faut mettre (a >> 16) & LOMASK) */
  142. #define LOWORD(a) (a & LOMASK)
  143. #define GLUE(hi, lo) ((hi << 16) + lo)
  144. #define SPLIT(a, b, c) b = HIWORD(a); c = LOWORD(a)
  145.  
  146.     ulong v1, v2, u3, u4, q1, q2, aux, aux1, aux2;
  147.     int k;
  148.     
  149.     for(k = 0; !(y & HIBIT); k++)
  150.         {
  151.             hiremainder <<= 1;
  152.             if (x & HIBIT) hiremainder++;
  153.             x <<= 1;
  154.             y <<= 1;
  155.         }
  156.         
  157.     SPLIT(y, v1, v2);
  158.     SPLIT(x, u3, u4);
  159.     
  160.     q1 = hiremainder / v1; if (q1 & HIMASK) q1 = LOMASK;
  161.     hiremainder -= q1 * v1;
  162.     aux = v2 * q1;
  163. again:
  164.     SPLIT(aux, aux1, aux2);
  165.     if (aux2 > u3) aux1++;
  166.     if (aux1 > hiremainder) {q1--; hiremainder += v1; aux -= v2; goto again;}
  167.     u3 -= aux2;
  168.     hiremainder -= aux1;
  169.     hiremainder <<= 16; hiremainder += u3 & LOMASK;
  170.     
  171.     q2 = hiremainder / v1; if (q2 & HIMASK) q2 = LOMASK;
  172.     hiremainder -= q2 * v1;
  173.     aux = v2 * q2;
  174. again2:
  175.     SPLIT(aux, aux1, aux2);
  176.     if (aux2 > u4) aux1++;
  177.     if (aux1 > hiremainder) {q2--; hiremainder += v1; aux -= v2; goto again2;}
  178.     u4 -= aux2;
  179.     hiremainder -= aux1;
  180.     hiremainder <<= 16; hiremainder += u4 & LOMASK;
  181.     hiremainder >>= k;
  182.     return GLUE(q1, q2);
  183. }
  184.  
  185.  
  186.  
  187. long mulmodll(a,b,c)
  188.      ulong a,b,c;
  189. {
  190.   divll(mulll(a,b),c);
  191.   return hiremainder;
  192. }
  193.